<?php
class Admin_update extends Admin_Controller
{

	function __construct()
	{
		parent::__construct();
		$this->load->model('Attach_mdl');
		$this->load->library('Form_validation');
		
		$this->uid = $this->user_login->is_logged_in() ? $this->user_login->get_user_id() : 0;
	}

	function index($path = '')
	{
		$data['path'] = $path;
		$this->load->view('update', $data);
	}
	
	function update($path = '')
	{
		$save_path = $path . DIRECTORY_SEPARATOR . date("Ymd", time()) . DIRECTORY_SEPARATOR;
		$upload_path = DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . $save_path;

		if(!is_dir('.'.$upload_path)){
			mkdir('.'.$upload_path,0777);
		}
		$file_name = mt_rand(0,time());
		$config['upload_path'] = '.'.$upload_path;
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '1000';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$config['file_name'] = $file_name.'.jpg';
		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload('image'))
		{
			$array = array(
				'id' => 1000,
				'fileName' => $this->upload->display_errors()
			);
		} 
		else
		{
			$data = $this->upload->data();
			unset($config);
			//生成缩略图
			$config['image_library'] = 'gd2';
			$config['source_image'] = '.'.$upload_path.$file_name.'.jpg';
			$config['quality'] = '65';
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['width']  = 650;
			$config['height'] = 400;
			$this->load->library('image_lib', $config); 
			$this->image_lib->resize();
			unlink('.'.$upload_path.$file_name.'.jpg');
			//阿里云
				$this->load->library('alioss');
				$tmp_name = dirname(dirname(dirname(dirname(__FILE__))));
		        $content = '';
		        $length = 0;
		        $fp = fopen($tmp_name.$upload_path.$file_name.'_thumb.jpg','r');
		        if($fp)
		        {
		            $f = fstat($fp);
		            $length = $f['size'];
		            while(!feof($fp))
		            {
		                $content .= fgets($fp);
		            }
		        }
		        $upload_file_options = array('content' => $content, 'length' => $length);
				$upload_file_by_content = $this->alioss->upload_file_by_content('ichufang',$save_path.$file_name.'_thumb.jpg', $upload_file_options);
			//阿里云结束
			$data_array = array(
				'time' => time(),
				'filename' => $data['file_name'],
				'filetype' => $data['image_type'],
				'filesize' => $data['file_size'],
				'location' => $save_path.$file_name.'_thumb.jpg',
				'isimage' => $data['is_image'],
				'uid' => $this->uid
			);
			$ins_id = $this->Attach_mdl->attach_insert($data_array);

			$array = array(
				'id' => 1000,
				'fileName' => server_img($save_path.$file_name.'_thumb.jpg') 
			);
		}

		$this->output->set_output(json_encode($array));
	}
	//编辑器上传接口
	function update_edit()
	{
		$save_path = 'edit' . DIRECTORY_SEPARATOR . date("Ymd", time()) . DIRECTORY_SEPARATOR ;
		$upload_path = DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . $save_path;

		if(!is_dir('.'.$upload_path)){
			mkdir('.'.$upload_path,0777);
		}
		$file_name = mt_rand(0,time());
		$config['upload_path'] = '.'.$upload_path;
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '1000';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$config['file_name'] = $file_name.'.jpg';
		  
		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload('filedata'))
		{
			$array = array(
				'err' => $this->upload->display_errors(),
				'msg' => ''
			);
		} 
		else
		{
			$data = $this->upload->data();
			//阿里云
				$this->load->library('alioss');
				$tmp_name = dirname(dirname(dirname(dirname(__FILE__))));
		        $content = '';
		        $length = 0;
		        $fp = fopen($tmp_name.$upload_path.$file_name.'.jpg','r');
		        if($fp)
		        {
		            $f = fstat($fp);
		            $length = $f['size'];
		            while(!feof($fp))
		            {
		                $content .= fgets($fp);
		            }
		        }
		        $upload_file_options = array('content' => $content, 'length' => $length);
				$upload_file_by_content = $this->alioss->upload_file_by_content('ichufang',$save_path.$file_name.'.jpg', $upload_file_options);
			//阿里云结束
			$data_array = array(
				'time' => time(),
				'filename' => $data['file_name'],
				'filetype' => $data['image_type'],
				'filesize' => $data['file_size'],
				'location' => $save_path.$file_name.'.jpg',
				'isimage' => $data['is_image'],
				'uid' => $this->uid
			);

			$ins_id = $this->Attach_mdl->attach_insert($data_array);

			$array = array(
				'err' => '',
				'msg' => server_img($save_path.$file_name.'.jpg')
			);
		}

		$this->output->set_output(json_encode($array));
	}
}

/* End of file user.php */
/* Location: ./application/controllers/user.php */